<?php
class Zendhlt_View_Helper_CmsSearch extends Zend_View_Helper_Abstract{
	
	public function cmsSearch($kindDisplay = null, $widthSearch = '36%', $slider = '') {
		// Thẻ div dùng để gom các selectbox thành dòng
		// Div 1 dùng khi có hiển thị slider
		$begin_div_group1 = "";
		$end_div_group1 = "";
		// Div 2 dùng khi không hiển thị slider
		$begin_div_group2 = "";
		$end_div_group2 = "";
		// Tên class dùng cho selectbox
		$classNameTop = 'topSelectbox';
		$classNameTop1 = '';
		$className = $classNameMargin = ' selectbox normal ';
		$classNameSmall = ' selectbox small ';
		$normal = 'normal';
		$class_button = 'submit';
		
		$xhtml = '<div id="slider_and_search">';
		if ($kindDisplay == "slider") {
			$xhtml .= '<div id="slider">';
			
			if ($slider != '') {
				$xhtml .= $slider;
			} else {
				$xhtml .= '<img src="' . $this->view->baseUrl(PUBLIC_PATH . '/images/slide_image.png') . '" alt="Image" />';
			}
			
			$xhtml .= '</div>';
			$begin_div_group2 = '<div class="row-search">';
			$end_div_group2 = '<br class="clear" /></div>';
		} else {
			$begin_div_group1 = '<div class="row-search">';
			$end_div_group1 = '<br class="clear" /></div>';
			$classNameTop = $classNameTop1 = 'topSelectbox2';
			$className = ' selectbox2 normal2 ';
			$classNameMargin = ' selectbox float-left normal2 ';
			$normal = 'normal2';
			$class_button = 'submit2';
		}
		$xhtml .= '<div id="search" style="width: ' . $widthSearch . ';">';
		$xhtml .= '<div class="title">Tìm kiếm nhanh<div class="icon_search"></div></div>';
		
		$form = new Default_Form_Search();
		
		// Selectbox loại tin
		$model = new Default_Model_LoaiTinMapper();
		$arrOptions = $model->fetchAll('da_xoa = 0');
		$key = 0;
		$value = ' -- Chọn loại tin -- ';
		$form->idloaitin->addMultiOption($key, $value);
		foreach($arrOptions as $val) {
			$key   = $val->id;
			$value = $val->ten;
			$form->idloaitin->addMultiOption($key, $value);
		}
		$form->idloaitin->setAttrib('class', $normal);
		
		// Selectbox tỉnh thành
		$model = new Default_Model_ThanhPhoMapper();
		$arrOptions = $model->fetchAll();
		$key = 0;
		$value = ' -- Tỉnh/thành -- ';
		$form->idtinhthanh->addMultiOption($key, $value);
		foreach($arrOptions as $val) {
			$key   = $val->id;
			$value = $val->ten;
			$form->idtinhthanh->addMultiOption($key, $value);
		}
		$form->idtinhthanh->setAttrib('class', 'small');
		$form->idtinhthanh->setAttrib('onchange', 'getDistrict(true, idtinhthanh, idquanhuyen)');
		
		// Selectbox quận huyện
		$key = 0;
		$value = ' -- Quận/huyện -- ';
		$form->idquanhuyen->addMultiOption($key, $value);
		$form->idquanhuyen->setAttrib('class', 'small');
		
		// Selectbox giá phòng
		$key = 0;
		$value = ' -- Chọn giá -- ';
		$form->idgia->addMultiOption($key, $value);
		$form->idgia->addMultiOption(9, 'Thỏa thuận');
		$form->idgia->addMultiOption(1, '< 1 triệu');
		$form->idgia->addMultiOption(2, '1 triệu - 2 triệu');
		$form->idgia->addMultiOption(3, '2 triệu - 4 triệu');
		$form->idgia->addMultiOption(4, '4 triệu - 6 triệu');
		$form->idgia->addMultiOption(5, '> 6 triệu');
		$form->idgia->setAttrib('class', $normal);
		
		// Selectbox diện tích
		$key = 0;
		$value = ' -- Chọn diện tích -- ';
		$form->iddientich->addMultiOption($key, $value);
		$form->iddientich->addMultiOption(10, '< 20m<sup>2</sup>');
		$form->iddientich->addMultiOption(11, '20 - 40m<sup>2</sup>');
		$form->iddientich->addMultiOption(12, '40 - 80m<sup>2</sup>');
		$form->iddientich->addMultiOption(13, '80 - 100m<sup>2</sup>');
		$form->iddientich->addMultiOption(14, '> 100m<sup>2</sup>');
		$form->iddientich->setAttrib('class', $normal);
		
		// Selectbox loại phòng
		$model = new Default_Model_LoaiPhongMapper();
		$arrOptions = $model->fetchAll('da_xoa = 0');
		$key = 0;
		$value = ' -- Loại phòng -- ';
		$form->idloaiphong->addMultiOption($key, $value);
		foreach($arrOptions as $val) {
			$key   = $val->id;
			$value = $val->ten;
			$form->idloaiphong->addMultiOption($key, $value);
		}
		$form->idloaiphong->setAttrib('class', 'small');
		
		// Selectbox số người ở
		$key = 0;
		$value = ' -- Số người ở -- ';
		$form->idsonguoio->addMultiOption($key, $value);
		for($i = 1; $i < 11; $i++) {
			$form->idsonguoio->addMultiOption($i, $i);
		}
		$form->idsonguoio->addMultiOption(11, '> 10 ');
		$form->idsonguoio->setAttrib('class', 'small');
		
		// Button search
		$form->submitSearch->setAttrib('class', $class_button);
		$form->submitSearch->setLabel('Tìm kiếm');
		
		if (Zend_Controller_Front::getInstance()->getRequest()->isPost()) {
			$formData = Zend_Controller_Front::getInstance()->getRequest()->getPost();
			if (isset($formData['submitSearch'])) {
				$modelDistricts = new Default_Model_QuanHuyenMapper();
				$arrOptions = $modelDistricts->fetchAll('thanhpho = ' . $formData['idtinhthanh']);
				$value = ' -- Quận huyện -- ';
				foreach($arrOptions as $val) {
					$form->idquanhuyen->addMultiOption($key, $value);
					$key   = $val->id;
					$value = $val->ten;
				}
				$form->idquanhuyen->addMultiOption($key, $value);
				$form->populate($formData);
			}
		}
		
		$xhtml .= '<div class="content">';
		$xhtml .= '<form id="' . $form->getId() . '" method="' . $form->getMethod() . '" action="' . $this->view->baseUrl('/timkiem/ketquatimkiem') . '" enctype="' . $form->getEnctype() . '" name="' . $form->getName() . '">';
		$xhtml .= $begin_div_group1;
		$xhtml .= $form->idloaitin;
		$xhtml .= $begin_div_group2;
		$xhtml .= $form->idtinhthanh;
		$xhtml .= $form->idquanhuyen;
		$xhtml .= $end_div_group2;
		$xhtml .= $form->idgia;
		$xhtml .= $end_div_group1;
		$xhtml .= $begin_div_group1;
		$xhtml .= $form->iddientich;
		$xhtml .= $begin_div_group2;
		$xhtml .= $form->idloaiphong;
		$xhtml .= $form->idsonguoio;
		$xhtml .= $end_div_group2;
		$xhtml .= $form->submitSearch;
		$xhtml .= $end_div_group1;
		$xhtml .= '</form>';
		
		$xhtml .= '<script type="text/javascript">';
		$xhtml .= "$(document).ready(function() {
			
			try {
					$('#idloaitin, #idgia').msDropDown({mainCSS:'dd2" . $className . $classNameTop . "'}).data('dd');
					$('#idtinhthanh, #idquanhuyen').msDropDown({mainCSS:'dd2" . $classNameSmall . $classNameTop1 . "'}).data('dd');
					$('#idgia').msDropDown({mainCSS:'dd2" . $classNameMargin . $classNameTop1 . "'}).data('dd');
					$('#iddientich').msDropDown({mainCSS:'dd2" . $className . "'}).data('dd');
					$('#idloaiphong, #idsonguoio').msDropDown({mainCSS:'dd2" . $classNameSmall . "'}).data('dd');
				} catch(e) {
					alert('Error: ' + e.message);
				}
			/*$('#submit').live('click', function(){
				var varLoaitin = $('#frmSearch #idloaitin').val();
				var varTinhthanh = $('#frmSearch #idtinhthanh').val();
				var varQuanhuyen = $('#frmSearch #idquanhuyen').val();
				var varGiatu = $('#frmSearch #idgiatu').val();
				var varGiaden = $('#frmSearch #idgiaden').val();
				var varDientich = $('#frmSearch #iddientich').val();
				var varLoaiphong = $('#frmSearch #idloaiphong').val();
				var varSonguoio = $('#frmSearch #idsonguoio').val();
				
				var postData ={
						'varLoaitin': varLoaitin,
						'varTinhthanh': varTinhthanh,
						'varQuanhuyen': varQuanhuyen,
						'varGiatu': varGiatu,
						'varGiaden': varGiaden,
						'varDientich': varDientich,
						'varLoaiphong': varLoaiphong,
						'varSonguoio': varSonguoio
				};
				event.preventDefault();
		        $.ajax({
		            type: 'POST',
		            url: '#',
		            async: true,
		            cache: false,
		            data: postData,
		            success: function(data){
		            	$('#primary').fadeOut(1000).fadeIn(1000);
		            }
		        });
		        return false;
		    });*/
		})";

		$xhtml .= '</script>';
		
		$xhtml .= '<br class="clear" /></div>';
		$xhtml .= '</div>';
		$xhtml .= '<br class="clear" /></div>';
		
		return $xhtml;
	}
}